import axios from 'axios';
// import router from '../router';
import {DOMAIN,BASE_PATH} from '../config/constant'


// 设置响应头content-type
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';


// 基础URL
let service = axios.create({
    baseURL: "/api",
    timeout: 5000,
    headers:{
        'Access-Control-Allow-Origin':'*'
    }
})



// 定义响应器
axios.interceptors.response.use(
    response => {
        if(response.status==200){
            return Promise.resolve(response);
        }else{
            return Promise.reject(response);
        }
    },
    error=>{
        if(error.response.status){
            switch(error.response.status){
                case 401:
                // router.replace({
                //     path: '/',
                //     query: {
                //         redirect: router.currentRoute.fullPath
                //     }
                // });
                break;
                case 404:
                    break;
            }
            return Promise.reject(error.response);
        }
    }
);

/**
 * 封装get方法
 */
export function get(url,params={}){
    return new Promise((resolve,reject)=>{
        service.get(url,{params:params})
        .then(response =>{
            resolve(response.data);
        })
        .catch(err => {
            reject(err);
        })
    });
}

export function post(url,data){
    return new Promise((resolve,reject)=>{
        service.post(url,data)
        .then(response => {
            resolve(response.data);
        })
        .catch(err =>{
            reject(err);
        })
    });
}
